package cc.rico.kafka.tutorial;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * Created by rico on 2018/6/30.
 */
public class KafkaProducerDemo {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", "192.168.223.140:9092");
        //properties.put("acks", "all");
        //properties.put("retries", 0);
        //properties.put("batch.size", 16384);
        //properties.put("linger.ms", 1);
        //properties.put("buffer.memory", 33554432);
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = null;
        try {
            producer = new KafkaProducer<String, String>(properties);
            for(int i=0; i<100; i++) {
                producer.send(new ProducerRecord<String, String>("test", i+"", "Kafka Message NO:"+i));
                System.out.println("[Produce]Kafka Message NO:" + i);
            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if(producer != null) {
                producer.close();
            }
        }


    }
}
